package com.kky.pp.common

import android.content.Context
import com.custom.base.config.BaseConfig
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.kky.pp.entity.BuyResultEnum
import com.kky.pp.popup.MemberBuyPopup
import com.kky.pp.entity.MemberSubscribeEntity
import com.kky.pp.pay.GooglePayUtils
import io.rong.imkit.SpName
import io.rong.imkit.entity.OrderCreateEntity
import com.kky.pp.callback.EmptySuccessCallBack

class MemberBuy {
    fun openMemberBuy(
        activity: Context,
        currentItem: Int,
        productCategory: Int = 1,
        targetId: String = "",
        callBackListener: (result: BuyResultEnum) -> Unit
    ) {
        MemberBuyPopup(
            activity,
            currentItem,
            object : MemberBuyPopup.ChangeMembershipListener {
                override fun onListener(bean: MemberSubscribeEntity.Data.ProductDescriptions) {
                    OkHttpManager.instance.requestInterface(
                        object : OkHttpFromBoy {
                            override fun addBody(requestBody: OkHttpBodyEntity) {
                                requestBody.setPost(Constant.user_create_order_url)
                                requestBody.add(
                                    "productCode",
                                    bean.productCode
                                )
                                requestBody.add(
                                    "productCategory",
                                    productCategory
                                )
                                requestBody.add("modelUserCode",targetId)
                            }

                        },
                        object :
                            SDOkHttpResoutCallBack<OrderCreateEntity>() {
                            override fun onSuccess(entity: OrderCreateEntity) {
                                GooglePayUtils.instance.start(entity, activity, object :
                                    EmptySuccessCallBack {
                                    override fun OnSuccessListener() {
                                        callBackListener.invoke(BuyResultEnum.SUCCESS)
                                        BaseConfig.getInstance.setBoolean(SpName.isMember, true)
                                    }

                                })
                            }

                            override fun onFailure(code: Int, msg: String) {
                                super.onFailure(code, msg)
                                callBackListener.invoke(BuyResultEnum.FAILURE)
                            }

                            override fun onFinish() {
                                super.onFinish()
                                callBackListener.invoke(BuyResultEnum.FINISH)
                            }
                        })
                }

                override fun closeListener(refreshTime: Long) {

                }

            })
    }
}